package com.jzo2o.foundations.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * TODO
 *
 * @Author anjing
 * @Date 2024/12/1 13:08
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    @Autowired
    private IServeItemService serveItemService;

    @Autowired
    private IRegionService regionService;

    @Override
    public List<Long> queryServeItemIdListByCityCode(String cityCode) {
        return null;
    }

    /**
     * @param serveUpsertReqDTOList 批量新增数据
     */
    @Override
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {

        for (ServeUpsertReqDTO dto : serveUpsertReqDTOList) {


            //判断服务项是否存在
            ServeItem item = serveItemService.getById(dto.getServeItemId());
            if (ObjectUtils.isEmpty(item)) {
                throw new ForbiddenOperationException("服务项不存在不能添加");
            }


            //判断服务项目是否未启动
            if (item.getActiveStatus() != FoundationStatusEnum.DISABLE.getStatus()) {
                throw new ForbiddenOperationException("服务项未启动");
            }


            //判断服务项目是否已添加
            Integer count = this.lambdaQuery()
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException("服务项目已启动");
            }


            //查询区域信息
            String cityCode = regionService.getById(dto.getRegionId()).getCityCode();

            //复制服务项为服务信息
            Serve serve = BeanUtils.copyBean(dto, Serve.class);
            serve.setCityCode(cityCode);

            //新增区域信息
            boolean result = this.save(serve);
            if (!result) {
                throw new DBException("添加服务项失败");
            }
        }
    }


    /**
     * 修改区域价格
     *
     * @param id    服务id
     * @param price 价格
     * @return
     */
    @Override
    public void update(Long id, BigDecimal price) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在，禁止修改");
        }
        //更新服务价格
        boolean update = this.lambdaUpdate()
                .set(Serve::getPrice, price)
                .eq(Serve::getId, id).update();
        if (ObjectUtils.isEmpty(update)) {
            throw new DBException("修改服务价格失败");
        }
    }

    /**
     * 是否热门
     *
     * @param id 服务id
     */
    @Override
    public void changeHotStatus(Long id) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在，禁止修改");
        }
        //判断是否有区域
        if (ObjectUtils.isEmpty(serve.getRegionId())) {
            throw new ForbiddenOperationException("区域不存在，禁止修改");
        }

       if(serve.getIsHot()==0){
           boolean update = this.lambdaUpdate()
                   .set(Serve::getIsHot, 1)
                   .eq(Serve::getId, id).update();
           if (ObjectUtils.isEmpty(update)) {
               throw new DBException("修改热门失败");
           }

       }else if(serve.getIsHot()==1){
           boolean update = this.lambdaUpdate()
                   .set(Serve::getIsHot,0)
                   .eq(Serve::getId, id).update();
           if (ObjectUtils.isEmpty(update)) {
               throw new DBException("修改热门失败");
           }

       }



    }

    @Override
    public int queryServeCountByRegionIdAndSaleStatus(Long regionId, Integer saleStatus) {
        return 0;
    }

    @Override
    public int queryServeCountByServeItemIdAndSaleStatus(Long serveItemId, Integer saleStatus) {
        return 0;
    }

    /**
     * 分页查询
     *
     * @param servePageQueryReqDTO 查询条件
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        //调用mapper查询数据，这里由于继承了ServiceImpl<ServeMapper, Serve>，使用baseMapper相当于使用ServeMapper
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    /**
     * 删除区域服务开发
     *
     * @param id 服务id
     */
    @Override
    public void deleteById(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在，禁止删除");
        }
        //判断是否有区域
        if (ObjectUtils.isEmpty(serve.getRegionId())) {
            throw new ForbiddenOperationException("区域不存在，禁止删除");
        }
        //判断服务是否上架
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus() && serve.getSaleStatus() == FoundationStatusEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("服务状态非草稿状态，禁止删除");
        }
        this.removeById(id);

    }

    @Override
    public List<Serve> queryHotAndOnSaleServeList() {
        return null;
    }

    @Override
    public ServeAggregationSimpleResDTO findDetailById(Long id) {
        return null;
    }

    @Override
    public List<ServeAggregationSimpleResDTO> findHotServeListByRegionId(Long regionId) {
        return null;
    }

    @Override
    public List<ServeAggregationTypeSimpleResDTO> findServeTypeListByRegionId(Long regionId) {
        return null;
    }

    /**
     * 上架
     *
     * @param id 服务id
     */
    @Override
    public void onSale(Long id) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在，禁止修改");
        }
        //判断是否有区域
        if (ObjectUtils.isEmpty(serve.getRegionId())) {
            throw new ForbiddenOperationException("区域不存在，禁止修改");
        }
        //判断是否上架
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务已上架，禁止修改");
        }
        //判断项目是否存在
        ServeItem item = serveItemService.getById(id);
        if (ObjectUtils.isNotEmpty(item)) {
            throw new ForbiddenOperationException("服务项不存在，禁止上架");
        }
        //修改状态为上架
        boolean update = this.lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .eq(Serve::getId, id).update();
        if (ObjectUtils.isEmpty(update)) {
            throw new DBException("修改上架失败");
        }

    }

    @Override
    public void offSale(Long id) {

        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在，禁止下架");
        }
        //判断是否有区域
        if (ObjectUtils.isEmpty(serve.getRegionId())) {
            throw new ForbiddenOperationException("区域不存在，禁止下架");
        }
        //判断是否上架
        if (serve.getSaleStatus() == FoundationStatusEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("服务已下架，禁止下架");
        }
        //判断项目是否存在
        ServeItem item = serveItemService.getById(id);
        if (ObjectUtils.isNotEmpty(item)) {
            throw new ForbiddenOperationException("服务项不存在，禁止下架");
        }
        //修改状态为上架
        boolean update = this.lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .eq(Serve::getId, id).update();
        if (ObjectUtils.isEmpty(update)) {
            throw new DBException("修改下架失败");
        }


    }

    @Override
    public ServeAggregationResDTO findServeDetailById(Long id) {
        return null;
    }

    @Override
    public boolean saveBatch(Collection<Serve> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Serve> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Serve> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Serve entity) {
        return false;
    }

    @Override
    public Serve getOne(Wrapper<Serve> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Serve> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Serve> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

//    @Override
//    public BaseMapper<Serve> getBaseMapper() {
//        return null;
//    }

    @Override
    public Class<Serve> getEntityClass() {
        return null;
    }
}
